using System;
using System.Data;
using System.IO;
using System.Web.UI.WebControls;
using NCS.Library.Components.Partner;
using NCS.Library.Utilities;
using NCS.Module.CMS.Library;
using NCS.Module.CMS.Library.Contents;
using NCS.Module.CMS.Library.Distributions;

namespace NCS.Module.CMS.Web
{
    public partial class PrintPreview : CMSWebControl
    {
        public int height = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (curDistInfo == null) return;
                ContentInfo content = ContentController.GetContent(curDistInfo.Distribution_ContentID);
                if (content == null) return;

                if (content.Content_SubTitle != String.Empty)
                {
                    lblSubTitile.Text = content.Content_SubTitle + " <br><br>";
                }
                else
                {
                    lblSubTitile.Visible = false;
                }
                if (content.Content_Alias != string.Empty && content.Content_Alias != "")
                {
                    lbdate.Text = " - " + ConvertUtility.FormatTimeVn(curDistInfo.Distribution_CreateDate);
                }
                else
                {
                    lbdate.Text = ConvertUtility.FormatTimeVn(curDistInfo.Distribution_CreateDate);
                }

                lblSpecialHeadline.Text = content.Content_Headline;
                lalTeaser.Text = content.Content_Teaser;
                litSpecialContentBody.Text = content.Content_Body;
                lbdescavatar.Text = content.Content_AvatarDescription;

                if (content.Content_Body.IndexOf("##BoxChuGiai##") > 0)
                {
                    litSpecialContentBody.Text = content.Content_Body.Replace("##BoxChuGiai##", content.Content_NoteBox);
                }

                if (content.Content_Avatar != string.Empty)
                {
                    imgSpecialAvatar.ImageUrl = content.Content_Avatar;
                    if (!System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory + imgSpecialAvatar.ImageUrl))
                        imgSpecialAvatar.Visible = false;
                }
                else
                {
                    imgSpecialAvatar.Visible = false;
                    divAvatar.Visible = false;
                }

                //chum anh
                DataTable dtPic = DistributionController.Distributions_GetPicInAlbumByContentId(curDistInfo.Distribution_ContentID);

                if (dtPic.Rows.Count > 0)
                {
                    string strAvatar = ConvertUtility.ToString(dtPic.Rows[0]["PicAvatar"]);
                    FileInfo fileInfo = new FileInfo(Server.MapPath(strAvatar));
                    System.Drawing.Image orImg = System.Drawing.Image.FromFile(fileInfo.FullName);
                    height = orImg.Height;

                    rptPic.DataSource = dtPic;
                    rptPic.DataBind();
                }
                else
                {
                    pnlSlide.Visible = false;
                }
            }
        }
        protected void rptPic_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView currow = (DataRowView)e.Item.DataItem;

                Image imgSlide = (Image)e.Item.FindControl("imgSlide");
                if (ConvertUtility.ToString(currow["PicAvatar"]) != string.Empty)
                {
                    imgSlide.ImageUrl = currow["PicAvatar"].ToString();
                }
                else
                {
                    imgSlide.Visible = false;
                }

                Label lblDesc = (Label)e.Item.FindControl("lblDesc");
                if (ConvertUtility.ToString(currow["PicDetail"]) != string.Empty)
                {
                    lblDesc.Text = currow["PicDetail"].ToString();
                }
                else
                {
                    lblDesc.Visible = false;
                }
            }
        }
    }
}